//
//  Device.m
//  DYLYJ
//
//  Created by JM on 15/10/24.
//  Copyright © 2015年 JM. All rights reserved.
//

#import "Device.h"
static int DeviceCount = 0;

@implementation XPGWifiDevice(MJ)

+ (NSArray *)mj_ignoredPropertyNames {
      return @[@"ui"];
}
//ui
//+ (NSDictionary *)mj_objectClassInArray
//{
//    return @{
//             @"ui" : [NSDictionary class],
//             };
//}
//+ (NSDictionary *)mj_replacedKeyFromPropertyName
//{
//    return @{@"did" : @"did_dy",
//             @"passcode" : @"passcode_dy",
//             };
//}


MJCodingImplementation
@end


@implementation Device
//
//+ (NSArray *)mj_ignoredPropertyNames {
//    return @[@"xpgDevice"];
//}

MJCodingImplementation
+ (instancetype)deviceWithIcon:(NSString *) aIconName name:(NSString *)aName state:(NSString *) aState open:(BOOL) aOpen {
    Device * device = [[Device alloc] init];
    DeviceCount++;
    device.macAddress = [NSString stringWithFormat:@"%d",DeviceCount];
    device.iconName = aIconName;
    device.name = aName;
    device.state = aState;
    device.open = aOpen;
//    [DYSMSCenter createSMSType:device.macAddress];
    
    return device;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _isHidden = NO;
    }
    return self;
}

- (void)setIconName:(NSString *)iconName {
    _iconName = iconName;
    NSDictionary *colorImageDic = @{@"device_1":@"#ffb100",
                                    @"device_2":@"#eb6100",
                                    @"device_3":@"#e5004f",
                                    @"device_4":@"#73077d",
                                    
                                    @"device_5":@"#0068b7",
                                    @"device_6":@"#ffb100",
                                    @"device_7":@"#129134",
                                    @"device_8":@"#7cc31f",};
      self.switchColor = colorImageDic[_iconName];//todo aIconName.png
    
//    if (_macAddress) {
//        [DYSMSCenter sendMessage:self byType:_macAddress];
//    }
}

+ (instancetype)deviceWith:(XPGWifiDevice*) axpgDevice {
    Device *device = [Device deviceWithIcon:@"device_1" name:@"未知设备" state:@"设备打开" open:YES ];
    device.xpgDevice = axpgDevice;
    return device;
}

- (void)setXpgDevice:(XPGWifiDevice *)xpgDevice {
    if (_xpgDevice) {
        if (![_xpgDevice.macAddress isEqualToString:xpgDevice.macAddress]){
            NSLog(@"！！！！bug 更换了真实设备");
        }
    }
    _xpgDevice = xpgDevice;
    self.macAddress = xpgDevice.macAddress;
    
    NSArray *array = [xpgDevice.remark componentsSeparatedByString:@"|#$&|"];
    if (array.count == 2){
        _name = array[0];
        self.iconName = array[1];
    }else {
        _name = xpgDevice.remark;
    }
    
//    [DYSMSCenter createSMSType:_macAddress];
//    if (_macAddress) {
//        [DYSMSCenter sendMessage:self byType:_macAddress];
//    }
}

//name 必须通过发绑定接口修改
//- (void)setName:(NSString *)name {
//    _name = name;
//    if (_xpgDevice){
//        _xpgDevice.remark = _name;
//    }
// 
//}

- (void)setDeviceReceiveData:(NSDictionary *)dataDic {
    _isClose_On = [dataDic[@"entity0"][@"Close_On"] boolValue];
    _isOpen_On = [dataDic[@"entity0"][@"Open_On"] boolValue];
    _isStop_On = [dataDic[@"entity0"][@"Stop_On"] boolValue];
    _isXiaodu_On = [dataDic[@"entity0"][@"Xiaodu_OnOff"] boolValue];
    _isFenggan_On = [dataDic[@"entity0"][@"Fenggan_OnOff"] boolValue];
    _isHonggan_On = [dataDic[@"entity0"][@"Honggan_OnOff"] boolValue];
    _isLED_On = [dataDic[@"entity0"][@"LED_OnOff"] boolValue];
    
    _isPower_on = [dataDic[@"entity0"][@"Auto_OnOff"] boolValue];
    _open = !_isOpen_On;
    [self updateState];
//    if (_macAddress) {
//        [DYSMSCenter sendMessage:self byType:_macAddress];
//    }
}

//- (void) dy_addObserver:(id<DYSMSProtocol>) aRecerver {
//    [DYSMSCenter addRecever:aRecerver withSMSType:_macAddress];
//}
//- (void) dy_removeObserver:(id<DYSMSProtocol>) aRecerver {
//    [DYSMSCenter removeReceiver:aRecerver withSMSType:_macAddress];
//}
//


#pragma mark-
- (void) setOpen:(BOOL)open {
    self.isClose_On = !open;
    self.isOpen_On = open;
}

- (void)updateState {
    _state = [NSString stringWithFormat:@"%@%@%@%@%@%@%@",
              (_isOpen_On&&!_isStop_On)?@" 衣架上升":@"",
              (_isClose_On&&!_isStop_On)?@" 衣架下降":@"",
              (_isStop_On)?@" 设备停":@"",
              _isXiaodu_On?@" 消毒":@"",
              _isFenggan_On?@" 风干":@"",
              _isHonggan_On?@" 烘干":@"",
              _isLED_On?@" 照明":@""];
}

//#define <#macro#>
- (void)setIsClose_On:(BOOL)isClose_On {
    _isClose_On = isClose_On;
    _open = NO;
    [self updateState];
}
- (void)setIsOpen_On:(BOOL)isOpen_On {
    _isOpen_On = isOpen_On;
     _open = YES;
    [self updateState];
}
- (void)setIsStop_On:(BOOL)isStop_On {
    _isStop_On = isStop_On;
    [self updateState];
}
- (void)setIsXiaodu_On:(BOOL)isXiaodu_On {
    _isXiaodu_On = isXiaodu_On;
    [self updateState];
}
- (void)setIsFenggan_On:(BOOL)isFenggan_On {
    _isFenggan_On = isFenggan_On;
    [self updateState];
}
- (void)setIsHonggan_On:(BOOL)isHonggan_On {
    _isHonggan_On = isHonggan_On;
    [self updateState];
}
- (void)setIsLED_On:(BOOL)isLED_On {
    _isLED_On = isLED_On;
    [self updateState];
}

//- (void)setName:(NSString *)name {
//    _name = name;
//    if (_macAddress) {
//        [DYSMSCenter sendMessage:self byType:_macAddress];
//    }
//}

@end
